    //
//  RegisterView.m
//  iFlower
//
//  Created by Hoang Thuc on 15/07/2010.
//  Copyright 2010 iNIL. All rights reserved.
//

#import "RegisterView.h"
#import "RegisterView2.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "Request.h"
#import "User.h"
#import "Country.h"
#import "Utility.h"

@implementation RegisterView

@synthesize fname;
@synthesize lname;
@synthesize address;
@synthesize city;
@synthesize state;
@synthesize phone;
@synthesize post_code;
@synthesize email;
@synthesize currentIndex;
@synthesize lblCountry;

//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//	return YES;
//}
- (void)resignResponder
{
	for (UIView* v in [self.view subviews]) {
		if([v isFirstResponder])
			[v resignFirstResponder];
	}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	[self resignResponder];
	[super touchesBegan:touches withEvent:event];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y - 180, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y + 180, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}

-(IBAction) buttonMenu
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
}
-(IBAction) buttonRegister
{
//	[[iFlowerAppDelegate shareDelegate] openSafariToRegister];
}
-(IBAction) buttonNext
{
	if([self.fname.text length]==0)
	{
		[Utility showAlert:@"Please enter first name"];
		[self.fname becomeFirstResponder];
		return;
	}
	if([self.lname.text length]==0)
	{
		[Utility showAlert:@"Please enter last name"];
		[self.lname becomeFirstResponder];
		return;
	}
	if([self.address.text length]==0)
	{
		[Utility showAlert:@"Please enter address"];
		[self.address becomeFirstResponder];
		return;
	}
	if([self.city.text length]==0)
	{
		[Utility showAlert:@"Please enter city"];
		[self.city becomeFirstResponder];
		return;
	}
	if([self.state.text length]==0)
	{
		[Utility showAlert:@"Please enter state"];
		[self.state becomeFirstResponder];
		return;
	}
	if([self.post_code.text length]==0)
	{
		[Utility showAlert:@"Please enter zip code"];
		[self.post_code becomeFirstResponder];
		return;
	}
	if([self.phone.text length]==0)
	{
		[Utility showAlert:@"Please enter phone number"];
		[self.phone becomeFirstResponder];
		return;
	}
	if([self.email.text length]==0)
	{
		[Utility showAlert:@"Please enter phone number"];
		[self.email becomeFirstResponder];
		return;
	}
	RegisterView2 *registerView2 = [[RegisterView2 alloc] initWithNibName:@"RegisterView2" bundle:nil];
	[self.navigationController pushViewController:registerView2 animated:YES];
	User* newUser = [User new];
	newUser.tittle = @"";
	newUser.firstName = fname.text;
	newUser.lastName = lname.text;
	newUser.address = address.text;
	newUser.city = city.text;
	newUser.state = state.text;
	newUser.country = [(Country*)[arrayCountry objectAtIndex:self.currentIndex] countryCode];
	newUser.zipcode = post_code.text;
	newUser.phone = phone.text;
	newUser.email = email.text;
	[[iFlowerAppDelegate shareDelegate] setNewAccount:newUser];
}
//--------
-(IBAction)buttonSelec_Country:(id)sender;
{
	[self resignResponder];
	actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
	[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
	CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
	pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
	pickerView.showsSelectionIndicator = YES;
	pickerView.dataSource = self;
	pickerView.delegate = self;
	[actionSheet addSubview:pickerView];
	//---
	UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
	closeButton.momentary = YES; 
	closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
	closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
	closeButton.tintColor = [UIColor blackColor];
	[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
	[actionSheet addSubview:closeButton];
	[closeButton release];

	[actionSheet showInView:self.view];

	[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}
-(void)dismissActionSheet:(id)sender
{
	[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
	self.lblCountry.text = [(Country*)[arrayCountry objectAtIndex:self.currentIndex] countryName];	
}

- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	arrayCountry = [[NSMutableArray alloc] initWithCapacity:[[[Request sharedRequest] entries]count]];
	for (NSDictionary* dictionary in [[Request sharedRequest] entries])
	{
		Country* entry = [[Country new] autorelease];
		if (entry != nil)
		{
			entry.countryName = [dictionary objectForKey: @"country"];
            NSLog(@"%@",[dictionary objectForKey:@"country_code"]);
			entry.countryCode = [dictionary objectForKey:@"country_code"];
			[arrayCountry addObject: entry];
		}
	}
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
	
}
- (void) contentFailed:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	NSLog(@"content failed");
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
}

- (void) applicationWillResignActive:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"applicationWillResignActive" object:nil];
	[self resignResponder];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[[iFlowerAppDelegate shareDelegate] showLoadingView];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillResignActive:) name: @"applicationWillResignActive" object: nil];

	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
	
	[[Request sharedRequest] listCountry];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[arrayCountry release];
	[lblCountry release];
	[fname release];
	[lname release];
	[address release];
	[state release];
	[city release];
	[phone release];
	[post_code release];
	[email release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
	
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
	
	return [arrayCountry count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	Country* c = [arrayCountry objectAtIndex:row] ;
	return [c countryName];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	
	self.currentIndex =  row;
}



@end
